package com.feijiang.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feijiang.entity.ExpeData;
import com.feijiang.mapper.ExpeDataMapper;
import com.feijiang.service.IExpeDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feijiang.util.MyPage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2021-12-28
 */
@Service
@Slf4j
public class ExpeDataServiceImpl extends ServiceImpl<ExpeDataMapper, ExpeData> implements
    IExpeDataService {

  @Value("${upload.file.directory}")
  String expeDataDir;

  @Resource
  ExpeDataMapper expeDataMapper;

  @Override
  public boolean add(int expeId, int creatorId, MultipartFile file) {
    // 先将文件保存到服务器指定路径
    // 生成时间戳文件前缀
    String now = DateUtil.now().replace(" ", "").replace(":", "").replace("-", "");
    String originalFilename = now +"_" + file.getOriginalFilename();
    File outFile = FileUtil.touch(expeDataDir + File.separator + originalFilename);
    // 写入到指定文件夹
    try {
      file.transferTo(outFile);
    } catch (IOException e) {
      return false;
    }
    // 路径保存到数据库
    ExpeData expeData = new ExpeData();
    expeData.setExpeId(expeId);
    expeData.setCreatorId(creatorId);
    expeData.setFilePath(originalFilename);
    int i = expeDataMapper.insert(expeData);
    return i > 0;
  }

  @Override
  public boolean deleteById(int id) {
    int i = expeDataMapper.deleteById(id);
    return i > 0;
  }

  @Override
  public boolean deleteByExpeId(int expeId) {
    int i = expeDataMapper.deleteByExpeId(expeId);
    log.info("删除实验数据集{}条", i);
    return i > 0;
  }

  @Override
  public List<ExpeData> queryByPage(int pageSize, int pageNum, int expeId) {
    return expeDataMapper.queryByPage(pageSize, (pageNum - 1) * pageSize, expeId);
  }

  @Override
  public MyPage listAllByPage(int pageSize, int pageNum) {
    List<ExpeData> expeDataList =  expeDataMapper.listAllByPage(pageSize, (pageNum - 1) * pageSize);
    return MyPage.buildPage(pageSize, pageNum, this.count(), expeDataList);
  }

  @Override
  public List<ExpeData> listAll() {
    QueryWrapper<ExpeData> expeDataQueryWrapper = new QueryWrapper<>();
    expeDataQueryWrapper.eq("is_checked", 1);
    return this.expeDataMapper.selectList(expeDataQueryWrapper);
  }

  @Override
  public MyPage listUnchecked(int pageSize, int pageNum) {
    List<ExpeData> expeDataList =  expeDataMapper.listUnchecked(pageSize, (pageNum - 1) * pageSize);
    QueryWrapper<ExpeData> expeDataQueryWrapper = new QueryWrapper<>();
    expeDataQueryWrapper.eq("is_checked", 0);
    return MyPage.buildPage(pageSize, pageNum, this.count(expeDataQueryWrapper), expeDataList);
  }

  @Override
  public boolean checkById(int id) {
    return expeDataMapper.checkById(id) > 0;
  }

  @Override
  public byte[] downloadByName(String name) {
      File file = new File(expeDataDir + File.separator + name);
    Assert.isTrue(file.exists(), "文件失效或不存在");
    return FileUtil.readBytes(file);
  }

  /**
   * 获取总条数
   *
   * @return
   */
  private long getCount() {
    return this.count();
  }
}
